package com.plan.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.plan.demo.entity.Duty;
import com.plan.demo.vo.CountPeopleVo;
import com.plan.demo.vo.PlanVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface DutyMapper extends BaseMapper<Duty> {
    @Select("TRUNCATE duty")
    void clear();

    @Select("select DISTINCT(day),IF(c.name is null,'无',c.name)  lightName,IF(c.type=1,'白班','夜班') light ,IF(e.name is null,'无',e.name)  nightName ,IF(e.type=1,'白班','夜班') night from duty a\n" +
            "left JOIN(\n" +
            "select b.user_id,b.day time,a.user_name `name`,b.type from duty b \n" +
            "LEFT JOIN `user` a on b.user_id = a.id\n" +
            "where b.type = 1\n" +
            ")c on a.`day` = c.time\n" +
            "LEFT JOIN(\n" +
            "select b.user_id,b.day time,a.user_name `name`,b.type from duty b \n" +
            "LEFT JOIN `user` a on b.user_id = a.id\n" +
            "where b.type = 2\n" +
            ")e on e.time = a.`day`")
    List<PlanVo> getPlan();

    @Select("SELECT user_id,IF(b.user_name is null,'无',b.user_name) user_name,IF(type=1,'白班','夜班') type,count(1) count FROM `duty` a\n" +
            "left join `user`  b on a.user_id = b.id\n" +
            "GROUP BY user_id,b.user_name,type")
    List<CountPeopleVo> getCountPeople();

    @Select("SELECT user_id,IF(b.user_name is null,'无',b.user_name) user_name,count(1) count FROM `duty` a\n" +
            "left join `user`  b on a.user_id = b.id\n" +
            "GROUP BY user_id,b.user_name")
    List<CountPeopleVo> getCountPeopleAll();
}
